#include "uzint_lab3_project.h"
#include <qfiledialog.h>
#include <qtextstream.h>
#include <cmath>

UZINT_lab3_Project::UZINT_lab3_Project(QWidget *parent)
    : QMainWindow(parent)
{
        ui.setupUi(this);

        ui.stackedWidget->setCurrentIndex(0);

        connect(ui.actionWybierz_plik, SIGNAL(triggered()), this, SLOT(openFilesystem()));
        connect(ui.actionO_programie, SIGNAL(triggered()), this, SLOT(about()));
	connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(showGroupStats(int)));
}

UZINT_lab3_Project::~UZINT_lab3_Project()
{

}

void UZINT_lab3_Project::showStats() {
        ui.stackedWidget->setCurrentIndex(1);

	QString labelText;
	labelText = labelText.number(model.totalNumberOfInodes());
	ui.label->setText(labelText);
	labelText = "";
	labelText = labelText.number(model.totalNumberOfBlocks());
	ui.label_noOfBlocks->setText(labelText);
	labelText = labelText.number(model.size());
	ui.label_fsSize->setText(labelText);
	labelText = labelText.number(model.numberOfGroups());
	ui.label_numberOfGroups->setText(labelText);
	labelText = labelText.number(model.totalNumberOfFreeBlocks());
	ui.label_freeBlocks->setText(labelText);
	labelText = labelText.number(model.totalNumberOfFreeInodes());
	ui.label_freeInodes->setText(labelText);
	labelText = labelText.number(model.freeSpace());
	ui.label_freeSpace->setText(labelText);
	labelText = labelText.number(model.blockSize());
	ui.label_blockSize->setText(labelText);

	QString comboboxlabel;
	for (int i = 0; i<model.numberOfGroups(); i++) {
		comboboxlabel = "";
		QTextStream(&comboboxlabel) << "Grupa numer " << i;
		ui.comboBox->insertItem(i, comboboxlabel, 0);
	}

	showGroupStats(0);
	double stosunek = model.totalNumberOfFreeBlocks() / (float)model.totalNumberOfBlocks();
	double procent = 1.0f - stosunek;
	int val = floor(procent * 100);
	ui.progressBar_total->setValue(val);


}

void UZINT_lab3_Project::openFilesystem()
{
	QString filename, labelText;
	filename = QFileDialog::getOpenFileName(this,
			                                "Open filesystem...",
			                                ".",
			                                "Filesystems (*.img)");
    labelText = labelText.number(model.openFilesystem(filename));
    ui.label_openedFile->setText(labelText == 0 ? "false" : "true");

    ui.comboBox->clear();

    emit showStats();
}

void UZINT_lab3_Project::showGroupStats(int groupNumber)
{
	QString labelText;

	labelText = labelText.number(model.numberOfBlocksInGroup(groupNumber));
	ui.label_blocksInAGroup->setText(labelText);
	labelText = labelText.number(model.numberOfFreeBlocksInAGroup(groupNumber));
	ui.label_freeBlocksInGroup->setText(labelText);
	labelText = labelText.number(model.numberOfFreeInodesInAGroup(groupNumber));
	ui.label_freeInodesInGroup->setText(labelText);
	labelText = labelText.number(model.numberOfInodesInAGroup());
	ui.label_inodesingroup->setText(labelText);

	int val = (1.0f - (model.numberOfFreeBlocksInAGroup(groupNumber)
			/ (float)model.numberOfBlocksInGroup(groupNumber))) * 100;
	ui.progressBar_group->setValue(val);

}

void UZINT_lab3_Project::about()
{
	QMessageBox::information(this, "O programie", "Program EXT2 Stats for UZINT Lab 3, wersja 1.0 final\n\nAutorzy:\nPiotr Kuszewski\nPawel Smolenski");
}
